/*var explodeImageReady = false;
var explodeImage = new Image();
explodeImage.onload = function () {
    explodeImageReady = true;
};
explodeImage.src = "images/explosion.gif";*/

Ship = function(x,y, theta, canvas, keycodes, scoreLocation, image, accelImage, color, respawnControlText){
    var self = this;
    this.upControl = keycodes[0];
    this.leftControl = keycodes[1];
    this.rightControl = keycodes[2];
    this.shootControl = keycodes[3];
    this.respawnControl = keycodes[4];
    this.score = 0;
    this.canShoot = true;
    this.respawnControlText = respawnControlText;
    
    this.imageReady = false;
    this.image = new Image();
    this.image.onload = function () {
        self.imageReady = true;
    };
    this.image.src = 'images/' + image;
	
	var laserSnd = new Audio('audio/laser.mp3');
	var blastSnd = new Audio('audio/blast.mp3');
	var thrusterSnd = new Audio('audio/thruster.mp3');
	thrusterSnd.loop=true;
    
    this.acceleratingImageReady = false;
    this.acceleratingImage = new Image();
    this.acceleratingImage.onload = function () {
        self.acceleratingImageReady = true;
    };
    this.acceleratingImage.src = 'images/' + accelImage;

    this.color= color;
    this.turnSpeed=20;
    this.speed=300;
    this.backSpeed=150;
	
    this.velocity = {
        y:0,
        x:0
    };
    this.velocityLimit = 3;
    this.canvas = canvas;
    
    this.isReady = false;
    this.dead = true;
    
    this.width=25;
    this.height=15;
    this.radius = this.width+this.height;
    this.accelerating = false;
    this.acceleration = 5;//   p/ms^2
    this.deceleration = -0.2;//  p/ms^2
    this.x=x;
    this.y=y;
    this.angle=theta;//Math.PI/4;
    this.scoreLocation = scoreLocation;
    this.smokes = new Array();
    this.timeSinceLastSmoke = 0;
    this.smokeInterval =.000000000005;
    this.smokeDensity = 3;
    
    this.bullets = new Array();
    
    this.shape = new Polygon(this.x,this.y, this.angle, new Array(
        new Point(-18, 30, '#76EE00'),
        new Point(27, 5, '#76EE00'),
        new Point(27, -5, '#76EE00'),
        new Point(-18, -30, '#76EE00')
        ), 50);

    this.draw= function(context){
        //also turned at the correct angle
        //context.clearRect(0, 0, canvas.width, canvas.height);
        if(this.isReady){
            context.save();
            context.translate(this.x,this.y);
            context.rotate(this.angle);
            if(!this.accelerating){
                context.drawImage(this.image, -this.image.width/2, -this.image.height/2);
            }else{
                context.drawImage(this.acceleratingImage, -this.acceleratingImage.width/2, -this.acceleratingImage.height/2);
            }
            context.restore();
            //this.shape.draw(context);
        
            var bulletCount = this.bullets.length;
            for(var i=0; i< bulletCount; i++){
                this.bullets[i].draw(context);
            }
            var smokeCount = this.smokes.length;
            for(var i=0; i< smokeCount; i++){
                this.smokes[i].draw(context);
            }
            var thetaMod = Math.atan( (this.height/2) / (this.width/2) );


            context.fillStyle = this.color;
            context.font = "bold 16px Arial";
            context.fillText("Score: " + this.score, this.scoreLocation.x, this.scoreLocation.y);
        }else{
            context.fillStyle = this.color;
            context.font = "bold 16px Arial";
            context.fillText("Press "+this.respawnControlText+" to join the game.", this.scoreLocation.x, this.scoreLocation.y);
        }

    };
    this.accelerate = function(modifier){
        //add velocity to the x and y components
        this.velocity.x += this.acceleration*modifier*Math.cos(this.angle);
        if(this.velocity.x >= this.velocityLimit){
            this.velocity.x = this.velocityLimit;
        }else if(this.velocity.x <= -this.velocityLimit){
            this.velocity.x = -this.velocityLimit;
        }
        this.velocity.y += this.acceleration*modifier*Math.sin(this.angle);
        if(this.velocity.y >= this.velocityLimit){
            this.velocity.y = this.velocityLimit;
        }else if(this.velocity.y <= -this.velocityLimit){
            this.velocity.y = -this.velocityLimit;
        }
    };
    this.decelerate = function(modifier){
        if(this.velocity.x < 0){
            //make velocity.x go to 0
            this.velocity.x += -this.deceleration*modifier;
        }else if(this.velocity.x > 0){
            //make velocity.x go to 0
            this.velocity.x += this.deceleration*modifier;
        }
        if(this.velocity.y < 0){
            //make velocity.y go to 0
            this.velocity.y += -this.deceleration*modifier;//*Math.sin(this.angle);
        }else if(this.velocity.y > 0){
            //make velocity.y go to 0
            this.velocity.y += this.deceleration*modifier;//*Math.sin(this.angle);
        }
    }
    this.update = function(modifier, keysDown){
        if(!this.dead){
            if (this.upControl in keysDown) { // Player holding up
                this.accelerate(modifier);
				thrusterSnd.play();
            }else{
				thrusterSnd.pause();
                this.decelerate(modifier);
            }
            if (this.leftControl in keysDown) { // Player holding left
                this.turnLeft(modifier);
            }
            if (this.rightControl in keysDown) { // Player holding right
                this.turnRight(modifier);
            }
        }else{
            this.angle += 10*modifier;
            this.decelerate(modifier);
            //addSmoke
            if(this.timeSinceLastSmoke - modifier > this.smokeInterval){
                //randomize x to be within the width/height
                for(var i=0; i<this.smokeDensity; i++){
                    var x =  Math.floor((Math.random()*(this.width))+1);
                    var y =  Math.floor((Math.random()*(this.height))+1);
                    this.smokes.push(new SmokeParticle(this.x-this.width/2 + x, this.y-this.height/2 + y));
                }
                this.timeSinceLastSmoke = 0;
            }else{
                this.timeSinceLastSmoke  += modifier;
            }
        }

        this.x = this.x + this.velocity.x;
        this.y = this.y + this.velocity.y;
        if(this.x > canvas.width){
            this.x = 0;
        }else if(this.x < 0){
            this.x = canvas.width;
        }
        if(this.y > canvas.height){
            this.y = 0;
        }else if(this.y < 0){
            this.y = canvas.height;
        }

        var bulletCount = this.bullets.length;
        for(var i=0; i< bulletCount; i++){
            this.bullets[i].update(modifier, keysDown);
            if(this.bullets[i].remove && this.bullets[i].remove(canvas) == true){
                this.bullets.remove(i);
                i--;
                bulletCount = this.bullets.length;
            }
        }
        
        var smokeCount = this.smokes.length;
        for(var i=0; i< smokeCount; i++){
            this.smokes[i].update(modifier);
            if(this.smokes[i].remove && this.smokes[i].remove(canvas) == true){
                this.smokes.remove(i);
                i--;
                smokeCount = this.smokes.length;
            }
        }
        
        this.shape.update(this.angle, new Point(this.x, this.y));
        
    };
    this.turnLeft= function(modifier){
        this.angle = this.angle - (Math.PI/10*modifier*this.turnSpeed);
        if(this.angle < 0){
            this.angle = Math.PI*2;
        }
    };
    this.turnRight= function(modifier){
        this.angle = this.angle + (Math.PI/10*modifier*this.turnSpeed);
        if(this.angle > Math.PI*2){
            this.angle = 0;
        }
    };
    this.destroy = function(){
        this.velocity.x /= 2;
        this.velocity.y /= 2;
        this.dead = true;
		blastSnd.play();
    };
    this.contains = function(point){
        return this.shape.contains(point);
    };
    this.scorePoint = function(){
        this.score++;
    };
    this.respawn = function(){
        if(!this.isReady){
            this.isReady = true;
        }
        this.x =  Math.floor((Math.random()*canvas.width)+1);
        this.y =  Math.floor((Math.random()*canvas.height)+1);
        this.velocity.x = 0;
        this.velocity.y = 0;
        this.angle = 0;
        this.dead = false;
        delete this.smokes;
        this.smokes = new Array();
    //randomize x inside the canvas
    //randomize y inside the canvas
    //make it not dead
    };

    addEventListener("keydown", function(e){
        if(e.keyCode == self.shootControl){
            if(!self.dead && self.canShoot){
                self.canShoot = false;
                self.bullets.push(new Bullet(self.x, self.y, self.angle, self.color));
				laserSnd.play();
            }
        }else if(e.keyCode == self.upControl){
            if(!self.dead){
                self.accelerating = true;
            }
        }else if(e.keyCode == self.respawnControl){
            if(self.dead){
                self.respawn();
            }
        }
    }, false);
    addEventListener("keyup", function(e){
        if(e.keyCode == self.upControl){
            self.accelerating = false;
        }else if(e.keyCode == self.shootControl){
            if(!self.dead){
                self.canShoot = true;
            }
        }
    }, false);
    
    

}
